import { opentelemetry } from '@elysiajs/opentelemetry';
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
import { resourceFromAttributes } from '@opentelemetry/resources';
import { ATTR_SERVICE_NAME, ATTR_SERVICE_VERSION } from '@opentelemetry/semantic-conventions';
import { PrismaInstrumentation } from '@prisma/instrumentation';

import { config } from '@/config';

// 设置 resource
const resource = resourceFromAttributes({
  [ATTR_SERVICE_NAME]: config.APP_NAME,
  [ATTR_SERVICE_VERSION]: config.APP_VERSION,
});

// 创建 OpenTelemetry 配置
// OpenTelemetry 会自动从标准环境变量中读取配置
const openTelemetryConfig = config.OTEL_ENABLED
  ? opentelemetry({
      resource,
      serviceName: config.APP_NAME,
      instrumentations: [new PinoInstrumentation(), new PrismaInstrumentation()],
    })
  : undefined;

/**
 * 创建 OpenTelemetry 配置
 *
 * @returns {ReturnType<typeof opentelemetry>} OpenTelemetry 配置
 */
export function createOpenTelemetryConfig(): ReturnType<typeof opentelemetry> | undefined {
  return openTelemetryConfig;
}

export * from '@opentelemetry/api';
